package com.nuanshui.heatedloan.credit.rabbitmq;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.credit.constant.BorrowType;
import com.nuanshui.heatedloan.credit.facade.CustomerCreditResultService;
import com.nuanshui.heatedloan.credit.facade.third.party.RiskManagerInteractionService;
import com.nuanshui.heatedloan.credit.form.CustomerCreditResultForm;
import com.nuanshui.heatedloan.credit.msg.CreditPushConstant;
import com.nuanshui.heatedloan.credit.msg.CreditPushMsgService;
import com.nuanshui.heatedloan.credit.response.RaiseLimitResponse;
import com.nuanshui.heatedloan.credit.vo.CustomerCreditResultVO;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageHandler;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.nuanshui.heatedloan.msgcenter.integration.CreditPushMsgBody;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author zss
 * @date 2017/7/15 10:19
 * @description 调用分控提额接口
 * @Since
 */
@MqMessageHandler(RabbitMqQueue.RABBIT_MQ_RAISE_LIMIT)
public class RaiseLimitHandler implements RabbitmqMessageHandler {

    private Logger logger =LoggerFactory.getLogger(RaiseLimitHandler.class);

    @Autowired
    public CustomerCreditResultService customerCreditResultService;

    @Autowired
    private RiskManagerInteractionService riskManagerInteractionService;

    //消息和推送
    @Autowired
    private CreditPushMsgService creditPushMsgService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        int amt=0;
        int upAmt=0;
        String userId="";
        try{
            LogUtil.info(logger, "----------执行风控提额接口开始----------");
            CustomerCreditResultForm form = new CustomerCreditResultForm();
            CustomerCreditResultVO customerCreditResultVO=new CustomerCreditResultVO();
            String msg = new String(message.getBody());
            if(SysStringUtils.isNotEmpty(msg)){
                RaiseLimitResponse raiseLimitResponse = JSON.parseObject(msg,RaiseLimitResponse.class);
                //调用另外一个队列  插入到http请求日志
                LogUtil.info(logger, "----------开始执行插入提额日志----------");
                riskManagerInteractionService.callBackRaiseLimit(raiseLimitResponse);
                LogUtil.info(logger, "----------执行插入提额日志完成----------");
                //开始调用风控提额接口
                int borrowType=Integer.parseInt(raiseLimitResponse.getBorrowType());
                userId=raiseLimitResponse.getCustomerId();
                customerCreditResultVO=customerCreditResultService.loadVOByUserId(userId);
                upAmt=raiseLimitResponse.getUpAmt();  //提额金额
                int allAmt=raiseLimitResponse.getTotalAmt();  //总金额
                LogUtil.info(logger, "---提额用户userId == ["+userId+"],要提额度upAmt == ["+upAmt+"]元");
                form.setUserId(userId);
                form.setTotalAmt(allAmt);
                if(Integer.parseInt(raiseLimitResponse.getBorrowType())== BorrowType.SINGLELOAN){
                    form.setSingleAmt(upAmt);
                    amt=customerCreditResultVO.getSingleAmt();
                }if(Integer.parseInt(raiseLimitResponse.getBorrowType())== BorrowType.INSTALLMENT){
                    form.setInstallmentAmt(upAmt);
                    amt=customerCreditResultVO.getInstallmentAmt();
                }
                customerCreditResultService.updateByUserId(form);
            }
            LogUtil.info(logger, "----------执行风控提额接口结束----------");
            //提额了，发送短信推送
            if(upAmt>amt){
            this.sendPushAndMsg(userId,upAmt-amt);
            }
        }catch (Exception e){
            e.printStackTrace();
            LogUtil.error(logger, "----------------------执行风控提额接口出错-----------------------");
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

    private void sendPushAndMsg(String userId,int upAmt){
        LogUtil.info(logger, "----------开始发送提额推送和短信----------");
        CreditPushMsgBody creditPushMsgBody =new CreditPushMsgBody();
        creditPushMsgBody.addDstUserId(userId);
        creditPushMsgBody.setType(CreditPushConstant.REPAY_TYPE);
        creditPushMsgBody.setOverduDay(0);
        creditPushMsgBody.setAmount(upAmt);
        creditPushMsgBody.setRemainDay(0);
        creditPushMsgService.dealPush(creditPushMsgBody);
        LogUtil.info(logger, "----------发送提额推送和短信完成----------");
    }
}
